package com.shanghaionstar.retrofit.Api;

import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.network.ClientTraceInstance;
import com.shanghaionstar.BuildConfig;
import com.shanghaionstar.retrofit.http.HttpManager;
import com.shanghaionstar.retrofit.listener.HttpOnNextListener;
import com.shanghaionstar.retrofit.listener.HttpOnNextSubListener;
import com.shanghaionstar.utils.ApplicationUtil;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import java.util.HashMap;
import java.util.Map;

import retrofit2.Retrofit;
import rx.Observable;

/**
 * 请求数据统一封装类
 * Created by WZG on 2016/7/16.
 */
public class HttpManagerApi extends BaseApi {
    private HttpManager manager;

    public HttpManagerApi(HttpOnNextListener onNextListener, RxAppCompatActivity appCompatActivity) {
        manager = new HttpManager(onNextListener, appCompatActivity);
    }

    public HttpManagerApi(HttpOnNextSubListener onNextSubListener, RxAppCompatActivity appCompatActivity) {
        manager = new HttpManager(onNextSubListener, appCompatActivity);
    }

    protected Retrofit getRetrofit() {
        return  manager.getReTrofit(getConnectionTime(), getBaseUrl());
    }


    protected void doHttpDeal(Observable observable) {
            manager.httpDeal(observable, this);
    }

    @Override
    public Observable getObservable(Retrofit retrofit) {
        return null;
    }

    @NonNull
    private StringBuilder getStringBuilder() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("ANDROID");
        stringBuilder.append("_");
        stringBuilder.append(Build.VERSION.RELEASE);
        stringBuilder.append("_");
        stringBuilder.append(BuildConfig.VERSION_CODE);
        stringBuilder.append("_");
        if (DeviceUtil.getLanguage() != null && DeviceUtil.getCountry() != null) {
            stringBuilder.append(DeviceUtil.getLanguage() + "-" + DeviceUtil.getCountry().toUpperCase());
        } else {
            stringBuilder.append("zh-CN");
        }
        stringBuilder.append("_");
        stringBuilder.append(DeviceUtil.getDeviceType());
        stringBuilder.append("_");
        stringBuilder.append(Constants.CLIENT_INFO_SECRET_ID);
        return stringBuilder;
    }


    public Map<String, String> getHeaders() {
        String clientTraceId = ClientTraceInstance.getInstance().getClientTraceId();
        StringBuilder stringBuilder = getStringBuilder();
        String token = ApplicationUtil.getInstance().getToken();
        String authorization = TextUtils.isEmpty(token) ? "Authorization" : token;
        Map<String, String> headers = new HashMap<>();
        headers.put("CLIENT-TRACE-ID", clientTraceId);
        headers.put("CLIENT-INFO", stringBuilder.toString());
        headers.put("Authorization", authorization);
        return headers;
    }
}
